package com.woniu.cloud.commentassembly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.cloud.entity.TReviewsImage;
import com.woniu.cloud.entity.TStoreReviews;
import com.woniu.cloud.commentassembly.mapper.TReviewsImageMapper;
import com.woniu.cloud.commentassembly.mapper.TStoreReviewsMapper;
import com.woniu.cloud.commentassembly.service.ITStoreReviewsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.cloud.common.dto.Result;
import com.woniu.cloud.form.InsertCommentForm;
import com.woniu.cloud.form.ReturnCommentForm;
import com.woniu.cloud.form.SelectCommentForm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 店铺评论表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2021-09-22
 */
@Service
@Slf4j
public class TStoreReviewsServiceImpl extends ServiceImpl<TStoreReviewsMapper, TStoreReviews> implements ITStoreReviewsService {



    @Resource
    private TStoreReviewsMapper storeReviewsMapper;

    @Resource
    private TReviewsImageMapper reviewsImageMapper;


    @Override
    public Result insertStoreReviews(InsertCommentForm storeReviewForm) {
        TStoreReviews storeReviews = new TStoreReviews();
        BeanUtils.copyProperties(storeReviewForm,storeReviews);
        log.info("storeReviews{}",storeReviews);
        storeReviews.setStoreReviewsTime(new Date())
                .setStoreReviewsStatus(1);
        int insert = storeReviewsMapper.insert(storeReviews);
        if (0!=insert) {
            ArrayList<String> imagePhoto = storeReviewForm.getReviewsImagePhoto();
            if(0!=imagePhoto.size()){
                imagePhoto.forEach(image->{
                    TReviewsImage reviewsImage = new TReviewsImage();
                    reviewsImage.setReviewsImageStoreReviewsId(storeReviews.getStoreReviewsId());
                    reviewsImage.setReviewsImagePhoto(image);
                    reviewsImageMapper.insert(reviewsImage);
                });
                log.info("添加成功->{}");
                return Result.success("评论成功",null);
            }

        }
        return Result.fail("评论失败",null);

    }

    @Override
    public Result deleteStoreReviewsByStoreReviewsId(Integer storeReviewsId) {
        log.info("正在删除->storePurchaseCarId:{}",storeReviewsId);
        int isDelete = storeReviewsMapper.deleteById(storeReviewsId);
        if (0!=isDelete) {
            QueryWrapper<TReviewsImage> wrapper = new QueryWrapper<>();
            wrapper.eq("reviews_image_store_reviews_id",storeReviewsId);
            reviewsImageMapper.delete(wrapper);
            log.info("删除成功->");
            return Result.success("删除成功",null);
        }
        log.info("删除失败->");
        return Result.fail("删除失败",null);

    }

    @Override
    public Result getStoreReviewsBySelectCommentForm(SelectCommentForm selectCommentForm) {
        log.info("查询评论->selectCommentForm:{}",selectCommentForm);
        QueryWrapper<TStoreReviews> wrapper = new QueryWrapper<>();
        wrapper.eq(null!=selectCommentForm.getStoreReviewsId(),"store_reviews_id",selectCommentForm.getStoreReviewsId())
                .eq(null!=selectCommentForm.getStoreReviewsStoreId(),"store_reviews_store_id",selectCommentForm.getStoreReviewsStoreId())
                .eq(null!=selectCommentForm.getStoreReviewsUserId(),"store_reviews_user_id",selectCommentForm.getStoreReviewsUserId())
                .eq(null!=selectCommentForm.getStoreReviewsReplyId(),"store_reviews_reply_id",selectCommentForm.getStoreReviewsReplyId())
                .eq(null!=selectCommentForm.getStoreReviewsStoreGoodId(),"store_reviews_store_good_id",selectCommentForm.getStoreReviewsStoreGoodId());
        Integer pageIndex = selectCommentForm.getPageIndex();
        IPage<TStoreReviews> page = new Page<>(null==pageIndex?1:pageIndex,8);
        IPage<TStoreReviews> selectPage = storeReviewsMapper.selectPage(page, wrapper);
        IPage<ReturnCommentForm> pageReturn = new Page<>();
        BeanUtils.copyProperties(page,pageReturn);
        List<ReturnCommentForm> commentForms = new ArrayList<ReturnCommentForm>();
        if (0!= selectPage.getRecords().size()) {
            selectPage.getRecords().forEach(storeReviews->{
                QueryWrapper<TReviewsImage> imageQueryWrapper = new QueryWrapper<>();
                imageQueryWrapper.eq("reviews_image_store_reviews_id",storeReviews.getStoreReviewsId());
                List<TReviewsImage> images = reviewsImageMapper.selectList(imageQueryWrapper);
                ReturnCommentForm commentForm = new ReturnCommentForm();
                commentForm.setStoreReviews(storeReviews);
                commentForm.setTReviewsImages(images);
                commentForms.add(commentForm);
            });
            pageReturn.setRecords(commentForms);
            return Result.success("存在数据",pageReturn);
        }
        return Result.fail("不存在数据",null);

    }
}
